package net.xt.testthread;

import java.util.Random;
import java.util.concurrent.PriorityBlockingQueue;

public class TestPriorityBlockingQueue {

    public static void main(String[] args) throws InterruptedException {
        // 指定初始化容量3，但是可以自行扩容
//        PriorityBlockingQueue<Object> queue = new PriorityBlockingQueue<>(3);

        PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<Integer>(
                3,
                (num1, num2) -> num2 - num1 // 设置排序，倒序排列
        );

        // 添加5个元素，队列自行扩容
        System.out.println("添加元素...");
        for (int i = 0; i < 5; i++) {
            int num = new Random().nextInt(100);
            queue.put(num);
            System.out.print(num + "  ");
        }

        System.out.println();

        // 获取元素，默认按升序排列
        System.out.println("获取元素...");
        for (int i = 0; i < 5; i++) {
            int num = queue.take();
            System.out.print(num + "  ");
        }
    }
}
